package com.guomao.demob;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class JavaDemo3 {

    public static void main(String[] args) {

//        method1();
//        method2();

        try {
            Class<?> clazz = Class.forName("com.guomao.demoa.Person");

//            Method[] ms = clazz.getMethods();
//            System.out.println(ms.length);

//            getDeclaredMethods获取非父类中的任意修饰符的成员方法对象
//            Method[] ms = clazz.getDeclaredMethods();
//            System.out.println(ms.length);
//            for (Method m : ms) {
//                System.out.println(m);
//            }

//            Constructor<?> c = clazz.getDeclaredConstructor();
//            Object obj = c.newInstance();

//            通过无参构造实例化对象
//            Object obj = clazz.newInstance();

            Constructor<?> conn = clazz.getDeclaredConstructor(String.class, int.class);
            Object obj = conn.newInstance("admin", 30);

//            Method method = clazz.getDeclaredMethod("sport", int.class);
//            obj.method();
//            方法对象.invoke(实例对象,参数列表);
//            method.invoke(obj, 60);
//            p.sport(60);

            Method m = clazz.getDeclaredMethod("study");
            m.setAccessible(true);
            Object result = m.invoke(obj);
            //void的方法返回值是null
            System.out.println(result);

//            Method m = clazz.getDeclaredMethod("showInfo");
//            Object result = m.invoke(obj);
//            System.out.println(result);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void method2() {
        try {
            Class clazz = Class.forName("com.guomao.demoa.Person");

            //获取构造方法

//            Constructor[] cons = clazz.getDeclaredConstructors();
//            System.out.println(cons.length);

            Constructor[] cons = clazz.getConstructors();
//            System.out.println(cons.length);

            for (Constructor con : cons) {
                System.out.println(con);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private static void method1() {
        //alt + shift +z
        try {
            Class clazz = Class.forName("com.guomao.demoa.Person");

            //获取构造函数对象 Constructor
//            Constructor[] constructors = clazz.getDeclaredConstructors();
//            System.out.println(constructors.length);

            Constructor con1 = clazz.getDeclaredConstructor();
//            System.out.println(con1);

            Constructor con2 = clazz.getDeclaredConstructor(String.class, int.class);
//            System.out.println(con2);

            //构造方法对象.newInstance(); 实例化
            Object o = con1.newInstance();
            System.out.println(o);

            Object o1 = con2.newInstance("admin", 20);
            System.out.println(o1);

            Constructor con3 = clazz.getDeclaredConstructor(String.class);
            //取消jvm访问权限检查
            con3.setAccessible(true);
            Object o2 = con3.newInstance("root");

            System.out.println(o2);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
